/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.NivelUsuario;


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 *
 * @author roderick
 */
public class NivelUsuarioModel extends DataBase{
    
    private int id;
    private String nusdesc;
    private String nusabre;

    public NivelUsuarioModel(){

    }

    private boolean validar(){        
        if(this.nusdesc.length()>20){
            JOptionPane.showMessageDialog(null,
            "Descripcion menor a 20 digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.nusabre.length()!=3){
            JOptionPane.showMessageDialog(null,
            "Codigo debe tener tres digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;

    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("insert into nivelusuario(nusdesc,nusabre) values(?,?);");
                stmt.setString(1,this.nusdesc);
                stmt.setString(2,this.nusabre);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("update nivelusuario set nusdesc = ? ,nusabre = ? where nuscod = ? ;");
                stmt.setString(1,this.nusdesc);
                stmt.setString(2,this.nusabre);
                stmt.setInt(3, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from nivelusuario where nuscod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT * FROM nivelusuario order by nusdesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT nuscod, nusdesc, nusabre FROM nivelusuario WHERE nusdesc LIKE '"+var+"%' order by nusdesc;");
    }

    public ResultSet finder(int var){
        return this.find("SELECT nuscod, nusdesc, nusabre FROM nivelusuario WHERE nuscod = "+var+" order by nusdesc;");
    }
    
    

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNusabre() {
        return nusabre;
    }

    public void setNusabre(String nusabre) {
        this.nusabre = nusabre;
    }

    public String getNusdesc() {
        return nusdesc;
    }

    public void setNusdesc(String nusdesc) {
        this.nusdesc = nusdesc;
    }   
}
